package com.xc.ezplan.core.base;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * @author lixingchen
 * @date 2022/11/15
 * @description 公共响应
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class EzResult<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	private static final String SUCCESS = "success";
	private static final String FAIL = "fail";

	@Getter
	@Setter
	private int code;

	@Getter
	@Setter
	private String msg;

	@Getter
	@Setter
	private T data;

	public static <T> EzResult<T> ok() {
		return restResult(null, HttpStatus.OK.value(), SUCCESS);
	}

	public static <T> EzResult<T> ok(T data) {
		return restResult(data, HttpStatus.OK.value(), SUCCESS);
	}

	public static <T> EzResult<T> ok(T data, String msg) {
		return restResult(data, HttpStatus.OK.value(), msg);
	}

	public static <T> EzResult<T> failed() {
		return restResult(null, HttpStatus.INTERNAL_SERVER_ERROR.value(), FAIL);
	}

	public static <T> EzResult<T> failed(String msg) {
		return restResult(null, HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
	}

	public static <T> EzResult<T> failed(T data) {
		return restResult(data, HttpStatus.INTERNAL_SERVER_ERROR.value(), FAIL);
	}

	public static <T> EzResult<T> failed(T data, String msg) {
		return restResult(data, HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
	}

	public static <T> EzResult<T> restResult(T data, int code, String msg) {
		EzResult<T> apiResult = new EzResult<>();
		apiResult.setCode(code);
		apiResult.setData(data);
		apiResult.setMsg(msg);
		return apiResult;
	}

}
